package ExtendDemo.demo03;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;

public class SendDemo {
    /**
     *  UDP发送数据的步骤
     *      1：创建发送端的socket对象（datagramSocket）
     *      2：创建数据，并把数据打包
     *      3：调用DatagramSocket对象的方法发送数据
     *      4：关闭发送端
     */
    public static void main(String[] args) throws IOException {

        DatagramSocket datagramSocket=new DatagramSocket();

        //自己封装键盘录入数据
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line= br.readLine())!=null){
            //输入的数据是886，发送数据结束
            if("886".equals(line)){
                break;
            }
            byte[] bytes = line.getBytes(StandardCharsets.UTF_8);
            DatagramPacket dp=new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.0.101"),12345);

            datagramSocket.send(dp);

        }
        datagramSocket.close();

    }
}
